# -*- coding: utf-8 -*-
"""
Version: 0.1
Author: CarpeDiem
Date: 2022/3/12
Description: 二维平面运动目标轨迹生成器
"""

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

# 方法一：根据目标的轨迹模型、轨迹参数完成轨迹输出
x = np.linspace(0, 2 * np.pi, 100)
y = np.sin(x)  # 给定轨迹模型

# 画轨迹曲线
fig = plt.figure(tight_layout=True)

# 设置坐标轴范围
plt.xlim((-1, 7))
plt.ylim((-2, 2))

# 画动点
point_ani, = plt.plot(x[0], y[0], "r-")
text_pt = plt.text(3.5, 0.8, '', fontsize=16)


def update(num):
    """
    更新数据点
    .set_data()是将这里的(x[num], y[num])代替上面的(x[0], y[0])
    """
    xx = np.linspace(0, 2 * np.pi * num / 100, num)  # 形成一个数组
    yy = np.sin(xx)
    point_ani.set_data(xx, yy)
    text_pt.set_text('x=%.3f, y=%.3f' % (x[num], y[num]))
    return point_ani, text_pt,


ani = animation.FuncAnimation(fig=fig,
                              func=update,
                              frames=np.arange(0, 100),
                              interval=80,
                              blit=True)

plt.show()

# 方法二：根据采集的数据的进行轨迹绘制
'''
import turtle as t
t.title('自动轨迹绘制')
t.setup(800, 600)
t.pencolor('red')
t.pensize(5)
t.speed(10)
# 数据读取
datals = []
with open('data.trace', 'rt') as fs:
    for line in fs:
        line = line.raplace('\n', '')
        datals.append(list(map(eval, line.split(','))))

for i in range(len(datals)):
    t.pencolor(datals[i][3], datals[i][4], datals[i][5])
    t.fd(datals[i][0])
    t.left(datals[i][2]) if datals[i][1] == 0 else t.right(datals[i][2])
t.done()
'''